<script setup>
import { ref } from 'vue';

const newTodo = ref('');
const todos = ref([
  { id: 1, text: '学习 Vue 基础知识', done: false },
  { id: 2, text: '创建一个简单的项目', done: false },
  { id: 3, text: '学习组件通信', done: false }
]);

function addTodo() {
  if (newTodo.value.trim()) {
    todos.value.push({
      id: Date.now(),
      text: newTodo.value,
      done: false
    });
    newTodo.value = '';
  }
}

function removeTodo(id) {
  todos.value = todos.value.filter(todo => todo.id !== id);
}

function toggleDone(id) {
  const todo = todos.value.find(todo => todo.id === id);
  if (todo) {
    todo.done = !todo.done;
  }
}
</script>

<template>
  <div class="todo-container">
    <h2>我的待办事项</h2>
    
    <div class="add-todo">
      <input 
        v-model="newTodo" 
        @keyup.enter="addTodo"
        placeholder="添加新的待办事项" 
      />
      <button @click="addTodo">添加</button>
    </div>
    
    <ul class="todo-list">
      <li v-for="todo in todos" :key="todo.id" :class="{ done: todo.done }">
        <input 
          type="checkbox" 
          :checked="todo.done" 
          @change="toggleDone(todo.id)" 
        />
        <span>{{ todo.text }}</span>
        <button @click="removeTodo(todo.id)">删除</button>
      </li>
    </ul>
    
    <div class="todo-stats">
      <p>总计: {{ todos.length }} 项 | 已完成: {{ todos.filter(t => t.done).length }} 项</p>
    </div>
  </div>
</template>

<style scoped>
.todo-container {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 20px;
}

.add-todo {
  display: flex;
  margin-bottom: 20px;
}

.add-todo input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.add-todo button {
  margin-left: 10px;
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.todo-list {
  list-style: none;
  padding: 0;
}

.todo-list li {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.todo-list li span {
  flex: 1;
  margin: 0 10px;
}

.todo-list li.done span {
  text-decoration: line-through;
  color: #999;
}

.todo-list button {
  padding: 4px 8px;
  background-color: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.todo-stats {
  margin-top: 20px;
  text-align: right;
  color: #666;
}
</style> 